# Read the data file and run analyses.

setwd("F:/Experiment Data/Complex Contagion/ComplexContagionDatasets/")
exp3 = read.csv("Exp3_polarizing_no_keep_five_neighbor.csv")

# Sanity checks.
head(exp3)
dim(exp3)
table(exp3$finished)
table(exp3$conditionName)

library(xtable)

# Code dependent variables.
# Code Self, Schis and Other columns.
exp3$Self = 0
exp3[exp3$userChoice == "Keep_10_cents_and_Do_Not_Donate", c("Self")] = 1
exp3$Schis = 0
exp3[exp3$userChoice == "Schistosomiasis_Control_Initiative", c("Schis")] = 1
exp3$Other = 0
exp3[exp3$Self == 0 & exp3$Schis == 0, c("Other")] = 1

# Code AnyCharity column.
exp3$AnyCharity = 0
exp3[exp3$userChoice != "Keep_10_cents_and_Do_Not_Donate", c("AnyCharity")] = 1

# Code independent variables.
# Code Similarity column.
exp3$Similarity = "High"
exp3[grepl("^AllLo__", exp3$conditionName), c("Similarity")] = "Low"
#exp3[, c("conditionName", "Similarity")]

# Code SelfNeighbors, SchisNeighbors and OtherNeighbors columns.
exp3$SelfNeighbors = 0
exp3[grepl("Self_1", exp3$conditionName), c("SelfNeighbors")] = 1
exp3[grepl("Self_2", exp3$conditionName), c("SelfNeighbors")] = 2
exp3[grepl("Self_3", exp3$conditionName), c("SelfNeighbors")] = 3

exp3$SchisNeighbors = 0
exp3[grepl("Schis_1", exp3$conditionName), c("SchisNeighbors")] = 1
exp3[grepl("Schis_2", exp3$conditionName), c("SchisNeighbors")] = 2
exp3[grepl("Schis_3", exp3$conditionName), c("SchisNeighbors")] = 3

exp3$OtherNeighbors = 0
exp3[grepl("Rand_1", exp3$conditionName), c("OtherNeighbors")] = 1
exp3[grepl("Rand_2", exp3$conditionName), c("OtherNeighbors")] = 2
exp3[grepl("Rand_3", exp3$conditionName), c("OtherNeighbors")] = 3

#exp3[, c("conditionName", "Similarity", "SelfNeighbors", "SchisNeighbors", "OtherNeighbors")]


# Run regressions.  Linear probability models.
# Influence only model.
influenceModel = glm(Schis ~ SchisNeighbors, family=binomial(link='logit'), data=exp3)
summary(influenceModel)

# Influence with tie-strength interaction model.
influenceSimilarityModel = glm(Schis ~ SchisNeighbors * Similarity, family=binomial(link='logit'), data=exp3)
summary(influenceSimilarityModel)
xtable(summary(influenceSimilarityModel))


# Demographics only model.
demographicsModel = glm(Schis ~ gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp3)
summary(demographicsModel)

# Influence + Similarity + Demographics model.
influenceDemographicsModel = glm(Schis ~ SchisNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp3)
summary(influenceDemographicsModel)


# Questions only model.
questionsModel = glm(Schis ~ restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp3)
summary(questionsModel)

# Influence + Similarity + Questions model.
influenceQuestionsModel = glm(Schis ~ SchisNeighbors * Similarity + restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp3)
summary(influenceQuestionsModel)

# Use questions as a liberal--conservative scale.
# First construct scale.
exp3$ConservativeAnswers = 0
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$restrict_gun_ownership == "no", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$climate_change == "no_big_deal", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$race_discrimination == "no_discrimination", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$homosexuality == "discourage", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$women_obstacles == "women_obstacles_no", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$abortion_murder == "yes", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$muslim_immigrants == "no", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$christian_nation == "yes", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$school_prayer == "yes", 1, 0)
exp3$ConservativeAnswers = exp3$ConservativeAnswers + ifelse(exp3$russian_influence == "no", 1, 0)

# Now try different models with that scale.
# Political scale only model.
scaleModel = glm(Schis ~ ConservativeAnswers, family=binomial(link='logit'), data=exp3)
summary(scaleModel)

# Influence with political scale model.
scaleInfluenceModel = glm(Schis ~ SchisNeighbors * ConservativeAnswers, family=binomial(link='logit'), data=exp3)
summary(scaleInfluenceModel)

# Influence + Similarity + political scale model.
scaleInfluenceSimilarityModel = glm(Schis ~ SchisNeighbors * Similarity * ConservativeAnswers, family=binomial(link='logit'), data=exp3)
summary(scaleInfluenceSimilarityModel)

